#include "Window.h"
#include "Font.h"

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
HDC             g_HDC;
_mouse          MouseCoord;
_WindowRect     WindowRect;

Window::Window(HINSTANCE hInstance,int x, int y, int width, int height)
{
    Done = false;

    WindowRect.height   = height;
    WindowRect.width    = width;
    WindowRect.x        = x;
    WindowRect.y        = y;

    /****************************************************
    *   Create Windows class and fill it up
    ****************************************************/
    windowClass.cbSize          = sizeof(WNDCLASSEX);
    windowClass.style           = CS_HREDRAW | CS_VREDRAW;
    windowClass.lpfnWndProc     = WndProc;
    windowClass.cbClsExtra      = 0;
    windowClass.cbWndExtra      = 0;
    windowClass.hInstance       = hInstance;
    windowClass.hIcon           = LoadIcon(NULL, IDI_APPLICATION);
    windowClass.hCursor         = LoadCursor(NULL, IDC_ARROW);
    windowClass.hbrBackground   = NULL;
    windowClass.lpszMenuName    = NULL;
    windowClass.lpszClassName   = "MyClass";
    windowClass.hIconSm         = LoadIcon(NULL, IDI_WINLOGO);

    RegisterClassEx(&windowClass);

    /****************************************************
    *   Create The actual window
    ****************************************************/
    hwnd = CreateWindowEx(NULL,
                    "MyClass",
                    "Titel balkje",
                    WS_OVERLAPPEDWINDOW | WS_VISIBLE |
                    WS_SYSMENU | WS_CLIPCHILDREN |
                    WS_CLIPSIBLINGS,
                    WindowRect.x, WindowRect.y,
                    WindowRect.width, WindowRect.height,
                    NULL,
                    NULL,
                    hInstance,
                    NULL);

    ShowWindow(hwnd,SW_SHOW);
    UpdateWindow(hwnd);

    StartupFontEngine();
}

Window::~Window()
{
    //dtor
}

void Window::Clear()
{
    PeekMessage(&msg, hwnd, NULL, NULL, PM_REMOVE);

    if(msg.message == WM_QUIT)
    {
        Done = true;
    }
}

void Window::Update()
{
    SwapBuffers(g_HDC);

    TranslateMessage(&msg);
    DispatchMessage(&msg);
}

void Window::PostQuit()
{
    Done = true;
}

bool Window::Complete()
{
    return Done;
}

void SetupPixelFormat(HDC hDC)
{
    int nPixelFormat;

    static PIXELFORMATDESCRIPTOR pfd = {
        sizeof(PIXELFORMATDESCRIPTOR),
        1,
        PFD_DRAW_TO_WINDOW |
        PFD_SUPPORT_OPENGL |
        PFD_DOUBLEBUFFER,
        PFD_TYPE_RGBA,
        32,
        0, 0, 0, 0, 0, 0,
        0,
        0,
        0,
        0, 0, 0, 0,
        16,
        0,
        0,
        PFD_MAIN_PLANE,
        0,
        0, 0, 0};

        nPixelFormat = ChoosePixelFormat(hDC, &pfd);
        SetPixelFormat(hDC, nPixelFormat, &pfd);
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    static HGLRC    hRC;
    static HDC      hDC;
    int width, height;

    switch(message)
    {
        case WM_CREATE:

            hDC = GetDC(hwnd);
            g_HDC = hDC;
            SetupPixelFormat(hDC);

            hRC = wglCreateContext(hDC);
            wglMakeCurrent(hDC, hRC);

            return 0;
            break;

        case WM_CLOSE:
            wglMakeCurrent(hDC, NULL);
            wglDeleteContext(hRC);

            PostQuitMessage(0);

            return 0;
            break;

        case WM_SIZE:
            height  = HIWORD(lParam);
            width   = LOWORD(lParam);

            if(height == 0)
            {
                height = 1;
            }

            glViewport(0, 0, width, height);
            glMatrixMode(GL_PROJECTION);
            glLoadIdentity();

            gluPerspective(45.0f, (GLfloat)width/(GLfloat)height,0.0f,100.0f);

            glMatrixMode(GL_MODELVIEW);
            glLoadIdentity();

            return 0;
            break;

        case WM_MOUSEMOVE:
            MouseCoord.mouseX = LOWORD(lParam);
            MouseCoord.mouseY = HIWORD(lParam);
            break;

        case WM_LBUTTONUP:
            MouseCoord.Left_Button = false;
            break;

        case WM_LBUTTONDOWN:
            MouseCoord.Left_Button = true;
            break;

        default:
            break;
    }

    return (DefWindowProc(hwnd,message,wParam,lParam));
}
